home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Programmer's Power Pack
/
Delphi Volume 1.iso
/
e_to_l
/
isamexpt
/
numctrl.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-09-15
|
18KB
|
688 lines
unit NumCtrl;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Menus, DsgnIntF;
{ string edit component }
type
TCustomStrEdit = class (TCustomEdit)
private
FAlignment: TAlignment;
FOldAlignment : TAlignment;
FTextMargin : integer;
FRightNull : Boolean;
function CalcTextMargin : integer;
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
procedure SetAlignment(Value: TAlignment);
protected
property Alignment: TAlignment read FAlignment write SetAlignment default taRightJustify;
property RightNull: Boolean read FRightNull write FRightNull default False;
procedure FormatText; dynamic;
procedure UnFormatText; dynamic;
public
constructor Create(AOwner: TComponent); override;
end;
TStrEdit = class (TCustomStrEdit)
published
property Alignment;
property AutoSize;
property BorderStyle;
property CharCase; {KB}
property Color;
property Ctl3D;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property MaxLength;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property RightNull; {KB}
property ShowHint;
property TabOrder;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
type
TNumericType = (ntGeneral, ntCurrency, ntPercentage);
TMaskString = string [25];
{ mask component }
type
TMasks = class (TPersistent)
private
FPositiveMask : TMaskString;
FNegativeMask : TMaskString;
FZeroMask : TMaskString;
FOnChange: TNotifyEvent;
protected
procedure SetPositiveMask (Value : TMaskString);
procedure SetNegativeMask (Value : TMaskString);
procedure SetZeroMask (Value : TMaskString);
public
constructor Create;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
published
property PositiveMask : TMaskString read FPositiveMask write SetPositiveMask;
property NegativeMask : TMaskString read FNegativeMask write SetNegativeMask;
property ZeroMask : TMaskString read FZeroMask write SetZeroMask;
end;
{ num edit component }
type
TCustomNumEdit = class (TCustomStrEdit)
private
FDecimals : word;
FDigits : word;
FMasks : TMasks;
FMax : extended;
FMin : extended;
FNumericType : TNumericType;
FUseRounding : boolean;
FValue : extended;
FValidate : boolean;
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure SetDecimals(Value : word);
procedure SetDigits(Value : word);
procedure SetMasks (Mask : TMasks);
procedure SetMax(Value : extended);
procedure SetMin(Value : extended);
procedure SetNumericType(Value : TNumericType);
procedure SetValue(Value : extended);
procedure SetValidate(Value : boolean);
protected
procedure FormatText; dynamic;
procedure KeyPress(var Key: Char); override;
procedure UnFormatText; dynamic;
property Decimals : word read FDecimals write SetDecimals;
property Digits : word read FDigits write SetDigits;
property Masks : TMasks read FMasks write SetMasks;
property Max : extended read FMax write SetMax;
property Min : extended read FMin write SetMin;
property NumericType : TNumericType read FNumericType write SetNumericType default ntCurrency;
property UseRounding : boolean read FUseRounding write FUseRounding;
property Value : extended read FValue write SetValue;
property Validate : boolean read FValidate write SetValidate;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function AsDouble : double; dynamic;
function AsInteger : integer; dynamic;
function AsLongint : longint; dynamic;
function AsReal : real; dynamic;
function AsString : string; dynamic;
procedure MaskChanged ( Sender : TObject );
function Valid ( Value : extended ) : boolean; dynamic;
end;
TNumEdit = class (TCustomNumEdit)
published
property AutoSize;
property BorderStyle;
property Color;
property Ctl3D;
property Decimals;
property Digits;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property Masks;
property Max;
property Min;
property NumericType;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property UseRounding;
property Value;
property Validate;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
implementation
type
TSetOfChar = set of char;
var
OldMaxLength : integer;
{========================================================================}
{ support routines }
{========================================================================}
function Power ( X, Y : integer ) : real;
begin
Result := exp ( ln ( X ) * Y );
end;
function StripChars ( const Text : string; ValidChars : TSetOfChar ) : string;
var
S : string;
i : integer;
Negative : boolean;
Begin
Negative := false;
if (Text [ 1 ] = '-') or (Text [length (Text)] = '-' ) then
Negative := true;
S := '';
for i := 1 to length ( Text ) do
if Text [ i ] in ValidChars then
S := S + Text [ i ];
if Negative then
Result := '-' + S
else
Result := S;
End;
{========================================================================}
{ Custom String Edit }
{========================================================================}
constructor TCustomStrEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAlignment := taLeftJustify;
FTextMargin := CalcTextMargin;
end;
function TCustomStrEdit.CalcTextMargin : integer;
{borrowed from TDBEdit}
{calculates a pixel offset from the edge of the control to the text(a margin)}
{used in the paint routine}
var
DC: HDC;
SaveFont: HFont;
I: Integer;
SysMetrics, Metrics: TTextMetric;
begin
DC := GetDC(0);
GetTextMetrics(DC, SysMetrics);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
I := SysMetrics.tmHeight;
if I > Metrics.tmHeight then
I := Metrics.tmHeight;
Result := I div 4;
end;
procedure TCustomStrEdit.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
Invalidate;
end;
end;
procedure TCustomStrEdit.CMEnter(var Message: TCMEnter);
begin
if FRightNull then UnformatText;
inherited;
FOldAlignment := FAlignment;
Alignment := taLeftJustify;
end;
procedure TCustomStrEdit.CMExit(var Message: TCMExit);
begin
if FRightNull then FormatText;
inherited;
Alignment := FOldAlignment;
end;
Procedure TCustomStrEdit.UnformatText;
begin
Text := StripChars ( Text, [ '0'..'9', Decima